package com.lzyjkz.quick_admin.web.controller.menu;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzyjkz.quick_admin.web.response.SysMenuComponentTreeNode;
import com.lzyjkz.quick_system.entity.vo.SysMenuNode;
import com.lzyjkz.quick_admin.web.response.SysMenuTreeNode;
import com.lzyjkz.quick_components.entity.Response;
import com.lzyjkz.quick_components.service.TokenService;
import com.lzyjkz.quick_system.entity.SysMenu;
import com.lzyjkz.quick_system.entity.enums.SysMenuTypeEnum;
import com.lzyjkz.quick_system.entity.enums.SysUserTypeEnum;
import com.lzyjkz.quick_system.security.SysUserDetails;
import com.lzyjkz.quick_system.service.SysMenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
@RequiredArgsConstructor
@Tag(name = "菜单管理")
public class SysMenuController {
    private final SysMenuService sysMenuService;
    private final TokenService tokenService;

    @PostMapping("/add")
    public Response<String> addMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Response.success("添加成功！");
    }

    @PostMapping("/update")
    public Response<String> updateMenu(@RequestBody SysMenu sysMenu) {
        boolean b = sysMenuService.updateById(sysMenu);
        if (b) {
            return Response.success();
        } else {
            return Response.fail("修改失败！");
        }
    }

    @PostMapping("/delete")
    public Response<String> deleteMenu(Long id) {

        boolean b = sysMenuService.recursiveDeleteById(id);
        return Response.success(null, b ? "删除成功" : "删除失败");
    }

    @GetMapping("/list")
    public Response<List<SysMenuComponentTreeNode>> listMenu(HttpServletRequest request) {
        SysUserDetails userD = (SysUserDetails) tokenService.getUserDetailsByHeader(request);
        if (SysUserTypeEnum.SUPER_ADMIN == userD.getUser().getType()) {
            List<SysMenu> list = sysMenuService
                    .lambdaQuery()
                    .groupBy(SysMenu::getFatherId, SysMenu::getOrderIndex, SysMenu::getId)
                    .orderBy(true, true, SysMenu::getOrderIndex)
                    .list();
            List<SysMenuComponentTreeNode> sysMenuComponentTreeNodes = new ArrayList<>();
            Map<Long, SysMenuComponentTreeNode> nodeMap = new HashMap<>();
            for (SysMenu sysMenu : list) {
                SysMenuComponentTreeNode sysMenuTreeNode = new SysMenuComponentTreeNode();
                sysMenuTreeNode.setLabel(sysMenu.getName());
                JSONObject value = new JSONObject();
                value.set("type", sysMenu.getType());
                value.set("componentUrl", sysMenu.getComponentUrl());
                value.set("viewUrl", sysMenu.getViewUrl());
                sysMenuTreeNode.setValue(value);
                sysMenuTreeNode.setId(sysMenu.getId());
                nodeMap.put(sysMenu.getId(), sysMenuTreeNode);
            }

            for (SysMenu sysMenu : list) {
                SysMenuComponentTreeNode sysMenuComponentTreeNode = nodeMap.get(sysMenu.getId());
                if (sysMenu.getFatherId() == 0L) {
                    sysMenuComponentTreeNodes.add(sysMenuComponentTreeNode);
                } else {
                    SysMenuComponentTreeNode fatherNode = nodeMap.get(sysMenu.getFatherId());
                    if (fatherNode.getChildren() == null) {
                        fatherNode.setChildren(new ArrayList<>());
                    }
                    fatherNode.getChildren().add(sysMenuComponentTreeNode);
                }
            }
            return Response.success(sysMenuComponentTreeNodes);
        }
        else {
            return null;
        }
    }

    @GetMapping("/tree")
    public Response<List<SysMenuTreeNode>> treeMenu(HttpServletRequest request) {
        SysUserDetails userD = (SysUserDetails) tokenService.getUserDetailsByHeader(request);
        if (SysUserTypeEnum.SUPER_ADMIN == userD.getUser().getType()) {
            List<SysMenu> list = sysMenuService.lambdaQuery().eq(SysMenu::getType, SysMenuTypeEnum.MENU).groupBy(SysMenu::getFatherId, SysMenu::getOrderIndex, SysMenu::getId).orderBy(true, true, SysMenu::getOrderIndex).list();
            List<SysMenuTreeNode> sysMenuTreeNodes = new ArrayList<>(List.of(new SysMenuTreeNode(0L, "主目录", 0L, null)));
            Map<Long, SysMenuTreeNode> nodeMap = new HashMap<>();
            for (SysMenu sysMenu : list) {
                SysMenuTreeNode sysMenuTreeNode = new SysMenuTreeNode();
                sysMenuTreeNode.setLabel(sysMenu.getName());
                sysMenuTreeNode.setValue(sysMenu.getId());
                sysMenuTreeNode.setId(sysMenu.getId());
                nodeMap.put(sysMenu.getId(), sysMenuTreeNode);
            }

            for (SysMenu sysMenu : list) {
                SysMenuTreeNode sysMenuTreeNode = nodeMap.get(sysMenu.getId());
                if (sysMenu.getFatherId() == 0L) {
                    sysMenuTreeNodes.add(sysMenuTreeNode);
                }
                else {
                    SysMenuTreeNode fatherNode = nodeMap.get(sysMenu.getFatherId());
                    if (fatherNode.getChildren() == null) {
                        fatherNode.setChildren(new ArrayList<>());
                    }
                    fatherNode.getChildren().add(sysMenuTreeNode);
                }
            }


            return Response.success(sysMenuTreeNodes);
        }
        return Response.success();
    }

    @GetMapping("/page")
    public Response<List<SysMenuNode>> getPage(Page<SysMenu> page, Long fatherId) {
        return Response.success(sysMenuService.getPage(page, fatherId));
    }

    @PostMapping("/search")
    public Response<List<SysMenuNode>> search(@RequestBody SysMenu sysMenu) {
        return null;
    }
}